package drive.Service.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.SecondHandItemMapper;
import drive.Model.Second;
import drive.Model.Secondhanditem;
import drive.Service.ServiceInterface.SecondHandItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SecondHandItemServiceA implements SecondHandItemService {
    @Autowired
    private SecondHandItemMapper secondHandItemMapper;

    private Gson gson = new Gson();

    @Override
    public String findAll() {
        List<Secondhanditem> list = secondHandItemMapper.findAll();
        return gson.toJson(list);
    }

    @Override
    public String findByItemID(int itemID) {
        Secondhanditem item = secondHandItemMapper.findByItemID(itemID);
        return gson.toJson(item);
    }

    @Override
    public String findBySellerID(int sellerID) {
        List<Secondhanditem> list = secondHandItemMapper.findBySellerID(sellerID);
        return gson.toJson(list);
    }

    @Override
    public String add(Secondhanditem item) {
        int result = secondHandItemMapper.add(item);
        return "发布商品成功";
    }

    @Override
    public String update(Second item) {
        Secondhanditem item1 =new Secondhanditem();
        item1.setSellerID(item.getSellerID());
        item1.setTitle(item.getTitle());
        item1.setDescription(item.getDescription());
        item1.setPrice(item.getPrice());
        item1.setCreationTime(new Date());
        item1.setStatus(item.getStatus());
        int result = secondHandItemMapper.update_noTime(item1);
        return "更新商品信息成功";
    }

    @Override
    public String delete(int itemID) {
        int result = secondHandItemMapper.delete(itemID);
        return "已删除商品";
    }
}